const BUTTON_COLOR_NORMAL = "#eee";
const BUTTON_COLOR_SELECTED = '#c3c3c3';

var userTable;
var userTableData;

function deleteUser(userIdToDelete) {
	if (userIdToDelete==undefined || userIdToDelete==null)
		return;
	if (!confirm("Delete this user?"))
		return;
	
	jQuery.post('delete_user.do', {userId : userIdToDelete}, openManageUsersTab());
}

function openRailroadControlTab() {
	selectMenuItem('#btnRailroad');
	userTable=null;
	userTableData=null;
	
	$('#div_railroad_controls').css('display', 'block');
	$('#div_manage_users').css('display', 'none');
}

function openManageUsersTab() {
	selectMenuItem('#btnManageUsers');
	jQuery.get('manage_users.html', function(text) {
		$('#div_manage_users').html(text);
		initUsersTable();
		
		$('#div_railroad_controls').css('display', 'none');
		$('#div_manage_users').css('display', 'block');
		
		jQuery.getJSON('get_user_list.do', function(userListJson) {
			var userList = userListJson; // eval('( ' + userListJson + ' )');
			var newData = [];
			for (i = 0; i < userList.length; i++) {
				var user = userList[i];
				var fullName = '';
				if (user.firstName)
					fullName = user.firstName;
				if (user.lastName) {
					if (user.firstName)
						fullName += ' ';
					fullName += user.lastName;
				}
				var row = [fullName, user.userName, user.role];
				newData.push(row);
			}
			
			userTableData.addRows(newData);
			userTable.draw(userTableData, {'showRowNumber': true});
		});
	});
}

function setUserTabContent(url) {
	jQuery.get(url, function(text) {
		$('#div_manage_users').html(text);
	});
}

function openAddUserTab() {
	jQuery.get('add_user.html', function(text) {
		$('#div_manage_users').html(text);
		$('#txtUserName').focus();
	});
}

function submitNewUser(dataForm) {
	jQuery.post('add_user.do', jQuery(dataForm).serialize(), openManageUsersTab());
}

function submitUserEdit(dataForm) {
	jQuery.post('edit_user.do', jQuery(dataForm).serialize(), openManageUsersTab());
}

function initUsersTable() {
	// Create and populate the userTableData table.
	userTableData = google.visualization.arrayToDataTable([
		['Name', 'User Name', 'Type']
	]);
	
	// Create and draw the userTable.
	userTable = new google.visualization.Table(document.getElementById('user_table'));
	userTable.draw(userTableData, {'showRowNumber': true});
	
	google.visualization.events.addListener(userTable, 'select', function() {
		var sel = userTable.getSelection();
		var selRow = sel[0].row;
		var selName = userTableData.getValue(selRow, 1);
		setUserTabContent('edit_user.jsp?user=' + selName);
	});
}

function focus(elemId) {
	$(elemId).focus();
}

function selectMenuItem(elemId) {
	$('#btnRailroad').css('background-color', BUTTON_COLOR_NORMAL);
	$('#btnManageUsers').css('background-color', BUTTON_COLOR_NORMAL);
	$('#btnChangePassword').css('background-color', BUTTON_COLOR_NORMAL);
	$(elemId).css('background-color', BUTTON_COLOR_SELECTED);
}
